package com.example.administrator.energyida.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.administrator.energyida.MainActivity;
import com.example.administrator.energyida.R;
import com.example.administrator.energyida.fragment.PersonalFragment;
import com.example.administrator.energyida.login.LoginActivity;
import com.j256.ormlite.stmt.query.In;
import com.jaeger.library.StatusBarUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PersonalMyinfoActivity extends Activity {

    private ListView listView;
    private List<Map<String, Object>> listData;
    private SimpleAdapter simpleAdapter;
    private TextView textViewBack;
    private Button buttonExit;
    private Button buttonSwitch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myinfo);

        initViews();

        // 设置状态栏的颜色
        StatusBarUtil.setColor(this, Color.parseColor("#ffffff"), 50);

        listData = new ArrayList<>();
        listData = getData();

        simpleAdapter = new SimpleAdapter(this,
                listData,
                R.layout.item_listview_personal,
                new String[]{"text", "img", "next"},
                new int[]{R.id.personal_listview_text,
                        R.id.personal_listview_img,
                        R.id.personal_listview_imageView_next});

        listView.setAdapter(simpleAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        Intent intent0 = new Intent();
                        intent0.setClass(PersonalMyinfoActivity.this, MyinfoRevisepasswordActivity.class);
                        startActivity(intent0);
                        break;
                    case 1:
                        Intent intent1 = new Intent();
                        intent1.putExtra("from", "MyInfo");
                        intent1.setClass(PersonalMyinfoActivity.this, FormCompanyInfoActivity.class);
                        startActivity(intent1);
                    default:
                        break;
                }
            }
        });

        buttonExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exit();
            }
        });
        buttonSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exit();
            }
        });

        textViewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initViews() {
        listView = findViewById(R.id.myinfo_listView);
        textViewBack = findViewById(R.id.text_myInfo_back);
        buttonSwitch = findViewById(R.id.personal_btn_switchUser);
        buttonExit = findViewById(R.id.personal_btn_exit);
    }

    private void exit() {
        SharedPreferences spAccount = getSharedPreferences("account", MODE_PRIVATE);
        SharedPreferences.Editor editorAccount = spAccount.edit();
        editorAccount.clear().commit();

        SharedPreferences spRemember = getSharedPreferences("remember", MODE_PRIVATE);
        SharedPreferences.Editor editorRemember =spRemember.edit();
        editorRemember.clear().commit();

        SharedPreferences spAuto = getSharedPreferences("auto", MODE_PRIVATE);
        SharedPreferences.Editor editorAuto = spAuto.edit();
        editorAuto.clear().commit();

        Intent intent = new Intent();
        intent.setClass(PersonalMyinfoActivity.this, LoginActivity.class);
        startActivity(intent);
    }

    private List<Map<String, Object>> getData() {

        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("img", R.mipmap.ic_launcher_round);
        map1.put("text", "修改密码");
        map1.put("next", R.drawable.next1);
        listData.add(map1);

        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("img", R.mipmap.ic_launcher_round);
        map2.put("text", "修改公司信息");
        map2.put("next", R.drawable.next1);
        listData.add(map2);

        return listData;
    }

}
